package org.streets.eis.component.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;
import org.streets.eis.component.AjaxFormComponentUpdatedBehavior;
import org.streets.eis.component.model.MapKeyModel;

import com.inmethod.grid.IGridColumn;
import com.inmethod.grid.column.CheckBoxColumn;
import com.inmethod.grid.column.PropertyColumn;
import com.inmethod.grid.toolbar.AbstractHeaderToolbar;

/**
 * 显示条件：Column为PropertyColumn的子类，且sortProperty不为空时
 * 如果有CheckBoxColumn,显示一个link用来清空全部过滤条件
 * 刷新时，会自动置空已选记录
 * 
 */
public class DefaultFilterToolbar extends AbstractHeaderToolbar{
	
	private static final long serialVersionUID = 1L;
	
	public DefaultFilterToolbar(TableList<?> tableList) {
		super(tableList,null);
		
		Map<String,String> filterData = new HashMap<String,String>();
		for (IGridColumn column : getActiveColumns()) {
			if (PropertyColumn.class.isAssignableFrom(column.getClass())){
				filterData.put(column.getId(), "");
			}	
		}		
		setDefaultModel(Model.ofMap(filterData));
		
		List<IGridColumn> list = new ArrayList<IGridColumn>();
		list.addAll(getActiveColumns());
		add(new ListView<IGridColumn>("items",list) {
			private static final long serialVersionUID = 1L;
			@Override
			protected void populateItem(ListItem<IGridColumn> item) {
				item.setRenderBodyOnly(true);
				item.add(populateFilterInput(item.getModel()));
			}
		});
	}
	
	protected Component populateFilterInput(IModel<IGridColumn> model){
		IGridColumn column = model.getObject(); 
		WebMarkupContainer item = new WebMarkupContainer("item");
		if (CheckBoxColumn.class.isAssignableFrom(column.getClass())){
			item.add(new AjaxLink<String>("filterInput"){
				private static final long serialVersionUID = 1L;
				
				@Override
				protected void onComponentTag(final ComponentTag tag){
					super.onComponentTag(tag);
					tag.setName("A");
					tag.remove("type");
					tag.put("href", "#");
					tag.put("title","点击清空所有过滤条件");
				}
				
				@Override
				protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag){
					CharSequence label = "清空"; 
					replaceComponentTagBody(markupStream, openTag,label);
				}
				
				@Override
				public void onClick(AjaxRequestTarget target) {
					DefaultFilterToolbar.this.clearFilters();
					DefaultFilterToolbar.this.refreshTableList();
				}				
			});
			item.add(new WebComponent("filterImg").setVisible(false));
		} else if (PropertyColumn.class.isAssignableFrom(column.getClass()) && !Strings.isEmpty(column.getSortProperty())){
			item.add(new TextField<String>("filterInput",new MapKeyModel<String,String>(getFilterValues(),column.getId())){
				private static final long serialVersionUID = 1L;
				@Override
				protected void convertInput(){
					String text = getRequest().getParameter(getId());
					setConvertedInput(text);
				}
			}.setMarkupId("filterInput_"+column.getId()).add(newFilterBehavior()));
			item.add(new WebComponent("filterImg"));
		} else {
			item.add(new TextField<String>("filterInput").setVisible(false));
			item.add(new WebComponent("filterImg").setVisible(false));
		}
		return item;
	}
	
	protected IBehavior newFilterBehavior(){
		return new AjaxFormComponentUpdatedBehavior("onchange"){
			private static final long serialVersionUID = 1L;
			@Override
			protected CharSequence getPostData(){
				return "\""+getComponent().getId() +	"=\" + Wicket.Form.encode(this.value)";
			}
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				DefaultFilterToolbar.this.refreshTableList();
				target.focusComponent(getComponent());
			}
		};		
	}
	
	protected void refreshTableList(){
		getTableList().resetSelectedItems();
		getTableList().ajaxRefresh();
	}
	
	
	@SuppressWarnings("unchecked")
	public Map<String,String> getFilterValues(){
		return (Map<String,String>) getDefaultModelObject();
	}
	
	public TableList<?> getTableList() {
		return (TableList<?>) getGrid();
	}
	
	public void clearFilters(){
		Map<String,String> model = getFilterValues();
		for (String key : model.keySet()){
			model.put(key,"");
		}
	}
	
}
